
package com.phevos.common.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.i18n.LocaleContextHolder;

import com.phevos.common.Constants;

public final class DateUtil
{
    private static char seperator = '-';

    private static long adjustTime = 0;

    //private static final String TIME_PATTERN = "HH:mm";

    private static Log log = LogFactory.getLog(DateUtil.class.getName());

    private DateUtil()
    {
    }

    public static long getDateMillis(String strDate)
    {
        if (strDate == null || (strDate != null && strDate.trim().equals("")))
        {
            return -1;
        }
        String sDate = strDate;
        String sTime = null;
        String sep = " ";
        StringTokenizer st = null;
        long millis = 0;
        st = new StringTokenizer(strDate, sep);
        if (st.hasMoreTokens())
        {
            sDate = st.nextToken();
        }
        if (st.hasMoreTokens())
        {
            sTime = st.nextToken();
        }
        int year = Integer.parseInt(sDate.substring(0, 4));
        int month = Integer.parseInt(sDate.substring(5, 7));
        int day = Integer.parseInt(sDate.substring(8));
        int hour = 0;
        int min = 0;
        int sec = 0;
        if (sTime != null)
        {
            st = new StringTokenizer(sTime, ":");
            if (st.hasMoreTokens())
            {
                hour = Integer.parseInt(st.nextToken());
            }
            if (st.hasMoreTokens())
            {
                min = Integer.parseInt(st.nextToken());
            }
            if (st.hasMoreTokens())
            {
                sec = Integer.parseInt(st.nextToken());
            }
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day, hour, min, sec);
        return calendar.getTimeInMillis();
    }

    /**
     * �Ѹ�ʽΪ yyyy/mm/dd �� yyyy-mm-dd ������ת���ɺ����� ���߰Ѹ�ʽΪ yyyy/mm/dd hh:mm:ss �� yyyy-mm-dd
     * hh:mm:ss ������ʱ��ת���ɺ�����
     * 
     * @param strDate ��ʾһ�������ַ���ʽΪ yyyy/mm/dd �� yyyy-mm-dd ��
     * @return ���ؾ���1970��1��1����ҹ0��ĺ�����
     */
    public static long getMillis(String strDate)
    {
        if (strDate == null || (strDate != null && strDate.trim().equals("")))
        {
            return -1;
        }
        String sDate = strDate;
        String sTime = null;
        String sep = " ";
        StringTokenizer st = null;
        long millis = 0;
        if (strDate.indexOf(":") > 0)
        {
            st = new StringTokenizer(strDate, sep);
            sDate = st.nextToken();
            sTime = st.nextToken();
        }
        sDate = getFormatDate(sDate);
        // ������ת��Ϊyyyymmdd��ʽ
        sDate = sDate.substring(0, 4) + sDate.substring(5, 7) + sDate.substring(8);
        // ��ʱ��ת��Ϊhhmmss��ʽ
        if (sTime != null)
        {
            sTime = sTime.substring(0, 2) + sTime.substring(3, 5) + sTime.substring(6);
        }
        else
        {
            sTime = "000000";
        }
        String strDateTime = sDate + sTime;
        return Long.parseLong(strDateTime);
    }

    /**
     * ����������ת���ɸ�ʽΪ yyyy-mm-dd�����ڡ�
     * 
     * @param millis ����1970��1��1����ҹ0��ĺ�����
     * @return ���ظ�ʽΪ yyyy-mm-dd���ַ����ں�ʱ�䡣
     */
    public static String getDate(long millis)
    {
        if (millis <= 0)
        {
            return "";
        }
        Calendar rightNow = getCalendar();
        rightNow.setTime(new java.util.Date(millis));
        int year = rightNow.get(Calendar.YEAR);
        int month = rightNow.get(Calendar.MONTH) + 1;
        int day = rightNow.get(Calendar.DAY_OF_MONTH);
        int hour = rightNow.get(Calendar.HOUR_OF_DAY);
        int minute = rightNow.get(Calendar.MINUTE);
        int second = rightNow.get(Calendar.SECOND);
        String strDateTime = year + "-" + (month < 10 ? "0" + month + "-" : month + "-")
                + (day < 10 ? "0" + day : day + "");
        return strDateTime;
    }

    /**
     * ����������ת���ɸ�ʽΪ yyyymmdd �����ڡ�
     * 
     * @param millis ����1970��1��1����ҹ0��ĺ�����
     * @return ���ظ�ʽΪ yyyymmdd���ַ����ں�ʱ�䡣
     */
    public static String getDate1(long millis)
    {
        if (millis <= 0)
            return "";
        Calendar rightNow = getCalendar();
        rightNow.setTime(new java.util.Date(millis));
        int year = rightNow.get(Calendar.YEAR);
        int month = rightNow.get(Calendar.MONTH) + 1;
        int day = rightNow.get(Calendar.DAY_OF_MONTH);
        int hour = rightNow.get(Calendar.HOUR_OF_DAY);
        int minute = rightNow.get(Calendar.MINUTE);
        int second = rightNow.get(Calendar.SECOND);
        String strDateTime = year + (month < 10 ? "0" + month : month + "")
                + (day < 10 ? "0" + day : day + "");
        return strDateTime;
    }

    /**
     * �Ѻ�����ת����һ�����ں�ʱ����ַ���ʽΪ��yyyy-mm-dd hh:mm:ss��
     * 
     * @param millis ����1970��1��1����ҹ0��ĺ�����
     * @return ���ظ�ʽΪ yyyy-mm-dd hh:mm:ss ���ַ����ں�ʱ�䡣
     */
    public static String getDateTime(long millis)
    {
        if (millis <= 0)
        {
            return "";
        }
        Calendar rightNow = getCalendar();
        rightNow.setTime(new java.util.Date(millis));
        int year = rightNow.get(Calendar.YEAR);
        int month = rightNow.get(Calendar.MONTH) + 1;
        int day = rightNow.get(Calendar.DAY_OF_MONTH);
        int hour = rightNow.get(Calendar.HOUR_OF_DAY);
        int minute = rightNow.get(Calendar.MINUTE);
        int second = rightNow.get(Calendar.SECOND);
        String strDateTime = year + "-" + (month < 10 ? "0" + month + "-" : month + "-")
                + (day < 10 ? "0" + day + " " : day + " ")
                + (hour < 10 ? "0" + hour + ":" : hour + ":")
                + (minute < 10 ? "0" + minute + ":" : minute + ":")
                + (second < 10 ? "0" + second : second + "");
        return strDateTime;
    }

    /**
     * ���ʱ���ַ� �ڶ����н�ʡ���� add by hehy �Ѻ�����ת����һ��϶̵����ں�ʱ����ַ���ʽΪ��mm-dd hh:mm��
     * 
     * @param millis ����1970��1��1����ҹ0��ĺ�����
     * @return ���ظ�ʽΪmm-dd hh:mm ���ַ����ں�ʱ�䡣
     */
    public static String getSimpleDateTime(long millis)
    {
        if (millis <= 0)
        {
            return "";
        }
        Calendar rightNow = getCalendar();
        rightNow.setTime(new java.util.Date(millis));
        int month = rightNow.get(Calendar.MONTH) + 1;
        int day = rightNow.get(Calendar.DAY_OF_MONTH);
        int hour = rightNow.get(Calendar.HOUR_OF_DAY);
        int minute = rightNow.get(Calendar.MINUTE);
        String strDateTime = (month < 10 ? "0" + month + "-" : month + "-")
                + (day < 10 ? "0" + day + " " : day + " ")
                + (hour < 10 ? "0" + hour + ":" : hour + ":")
                + (minute < 10 ? "0" + minute : minute + "");
        return strDateTime;
    }

    /**
     * ���ص�ǰ���ڵ��ַ���ʽ����ʽΪ��yyyy-mm-dd
     * 
     * @return �����yyyy-mm-dd��ʽ
     */
    public static String getToday()
    {
        Calendar rightNow = getCalendar();
        int year = rightNow.get(Calendar.YEAR);
        int month = rightNow.get(Calendar.MONTH) + 1;
        int day = rightNow.get(Calendar.DAY_OF_MONTH);
        return year + String.valueOf(seperator) + (month < 10 ? "0" + month : month + "")
                + String.valueOf(seperator) + (day < 10 ? "0" + day : day + "");
    }

    /**
     * ����MMDDYY��ʽ�ĵ�ǰ���ڡ�����:���赱����2004��8��11�գ��򷵻ص���08112004
     * 
     * @return String ��ǰ���ڵ�MMDDYY��ʽ
     */
    public static String getMMDDYY()
    {
        String strYY = ("" + getYear()).substring(2);
        int intMM = getMonth();
        String strMM = null;
        if (intMM < 10)
        {
            strMM = "0" + intMM;
        }
        else
        {
            strMM = "" + intMM;
        }

        String strDD = null;
        int intDD = getDay();
        if (intDD < 10)
        {
            strDD = "0" + intDD;
        }
        else
        {
            strDD = "" + intDD;
        }
        return strMM + strDD + strYY;
    }

    /**
     * ���ص�ǰ���
     * 
     * @return ��ǰ�����
     */
    public static int getYear()
    {
        Calendar rightNow = getCalendar();
        return rightNow.get(Calendar.YEAR);
    }

    /**
     * ���ص�ǰ�·�
     * 
     * @return int ��ǰ���·�
     */
    public static int getMonth()
    {
        Calendar rightNow = getCalendar();
        return rightNow.get(Calendar.MONTH) + 1;
    }

    /**
     * ���ص�ǰ��һ���е�����
     * 
     * @return int �����������
     */
    public static int getDay()
    {
        Calendar rightNow = getCalendar();
        return rightNow.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * ���ص�ǰ���ڼ�
     * 
     * @return int ��ǰ���ڼ�
     */
    public static int getWeekDay()
    {
        Calendar rightNow = getCalendar();
        return rightNow.get(Calendar.DAY_OF_WEEK) - 1;
    }

    /**
     * ����ָ�����ڵ����
     * 
     * @param millis ��������
     * @return int ���
     */
    public static int getYear(long millis)
    {
        String strDate = String.valueOf(millis);
        String year = strDate.substring(0, 4);
        return Integer.parseInt(year);
    }

    /**
     * ����ָ�����ڵ��·�
     * 
     * @param millis ��������
     * @return int �·�
     */
    public static int getMonth(long millis)
    {
        String strDate = String.valueOf(millis);
        String month = strDate.substring(4, 6);
        return Integer.parseInt(month);
    }

    /**
     * ����ָ�����������·��е�����
     * 
     * @param millis ��������
     * @return int ����
     */
    public static int getDay(long millis)
    {
        if (millis <= 0)
            return -1;
        String strDate = String.valueOf(millis);
        String day = strDate.substring(6, 8);
        return Integer.parseInt(day);
    }

    /**
     * �ж�}�����֮����������
     * 
     * @param millis1 �������ʾ�ĵ�һ��ʱ�䡣
     * @param millis2 �������ʾ�ĵڶ���ʱ�䡣
     * @return ����}��ʱ����������
     * @return int ��������
     */
    public static int getIntervalDays(long millis1, long millis2)
    {
        long beginMillis1 = DateUtil.getOneDayBeginMillis(millis1);
        long beginMillis2 = DateUtil.getOneDayBeginMillis(millis2);
        long realMillis1 = getDateMillis(beginMillis1);
        long realMillis2 = getDateMillis(beginMillis2);
        return Math.round((realMillis1 - realMillis2) / 86400000);
    }

    /**
     * ���һ���׼ʱ���һ�����׼ʱ���������� �õ�һ����ǰ������ʱ��
     * 
     * @param baseMillis ���������͵Ļ�׼ʱ��
     * @param days ���׼ʱ����������
     * @return ��ǰ������ʱ�������
     */
    public static long getAdvancedMillis(long baseMillis, int days)
    {
        // ȡ������ĺ�����
        long realMillis = getDateMillis(baseMillis);
        realMillis += days * 86400000l;
        return getLongDateTime(realMillis);
    } // end getAdvancedMillis(long baseMillis, int days)

    /**
     * ���һ���׼ʱ���һ�����׼ʱ���������� �õ�һ����ǰ������ʱ��
     * 
     * @param baseDate �ַ������͵Ļ�׼ʱ��
     * @param days ���׼ʱ����������
     * @return ��ǰ������ʱ�������
     */
    public static long getAdvancedMillis(String baseDate, int days)
    {
        // ȡ������ĺ�����
        long realMillis = dateMillis(baseDate);
        realMillis += days * 86400000l;
        return getLongDateTime(realMillis);
    } // end getAdvancedMillis(long baseMillis, int days)

    /**
     * ȡ��һ�����ʼ��������
     * 
     * @param millis ��������
     * @return long ��ʼʱ��ĺ�����
     */
    public static long getOneDayBeginMillis(long millis)
    {
        if (millis <= 0)
            return millis;
        String strDate = String.valueOf(millis);
        if (strDate.length() < 8)
            return -1;
        strDate = strDate.substring(0, 8) + "000000";
        return Long.parseLong(strDate);
    }

    /**
     * ȡ��һ�����ֹ��������
     * 
     * @param millis ��������
     * @return long ��ֹʱ��ĺ�����
     */
    public static long getOneDayEndMillis(long millis)
    {
        if (millis <= 0)
            return millis;
        String strDate = String.valueOf(millis);
        if (strDate.length() < 8)
            return -1;
        strDate = strDate.substring(0, 8) + "235959";
        return Long.parseLong(strDate);
    }

    /**
     * ȡ��һ�����ʼ��������
     * 
     * @param strDate �����ַ���ʽΪ��yyyy-mm-dd �� yyyy/mm/dd
     * @return long ��ֹʱ��ĺ�����
     */
    public static long getOneDayBeginMillis(String strDate)
    {
        if (strDate == null || (strDate != null && strDate.trim().equals("")))
            return -1;
        int len = strDate.length();
        if (len < 4)
            return -1;
        else if (len == 4) // yyyy
            strDate = strDate + "0101000000";
        else if (len <= 7) // yyyy-mm
            strDate = strDate.substring(0, 4) + strDate.substring(5, 7) + "01000000";
        else
            strDate = strDate.substring(0, 4) + strDate.substring(5, 7) + strDate.substring(8, 10)
                    + "000000";
        return Long.parseLong(strDate);
    }

    /**
     * ȡ��һ�����ֹ��������
     * 
     * @param strDate �����ַ���ʽΪ��yyyy-mm-dd �� yyyy/mm/dd
     * @return long ��ֹʱ��ĺ�����
     */
    public static long getOneDayEndMillis(String strDate)
    {
        if (strDate == null || (strDate != null && strDate.trim().equals("")))
            return -1;
        int len = strDate.length();
        if (len < 4)
            return -1;
        else if (len == 4) // yyyy
            strDate = strDate + "1231235959";
        else
            strDate = strDate.substring(0, 4) + strDate.substring(5, 7) + strDate.substring(8, 10)
                    + "235959";
        return Long.parseLong(strDate);
    }

    /**
     * ���Ǳ�׼���ڸ�ʽת��Ϊ��׼���ڸ�ʽ����׼���ڸ�ʽ��yyyy-mm-dd��
     * 
     * @param strDate String �Ǳ�׼���ڸ�ʽ�����ַ�
     * @return String ��׼���ڸ�ʽ�����ַ�
     */
    public static String getFormatDate(String strDate)
    {
        String sep = null;
        if (strDate.indexOf("-") > 0)
            sep = "-";
        else if (strDate.indexOf("/") > 0)
            sep = "/";
        StringTokenizer st = new StringTokenizer(strDate, sep);
        String year = st.nextToken();
        String month = st.nextToken();
        String day = st.nextToken();
        if (year == null || month == null || day == null)
            return "";
        if (year.length() == 2)
        {
            // ������>=70������Ϊ�� 19xx
            if (year.compareTo("70") >= 0)
                year = "19" + year;
            // ������<70������Ϊ�� 20xx
            else
                year = "20" + year;
        }
        if (month.length() == 1)
            month = "0" + month;
        if (day.length() == 1)
            day = "0" + day;

        return year + "-" + month + "-" + day;
    } // end getFormatDate(String strDate)

    /**
     * �ж�ĳ���Ƿ�Ϊ����
     * 
     * @param year �꣨���Σ�
     * @return ture����ʾδ���ꣻfalse��ʾ��������
     */
    public static boolean isLeapYear(int year)
    {
        if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * �ж�ĳ���Ƿ�Ϊ����
     * 
     * @param year �꣨���Σ�
     * @return ture����ʾδ���ꣻfalse��ʾ��������
     */
    public static boolean isRunNian(int year)
    {
        if ((year % 4 == 00) && (year % 100 != 0) || (year % 400 == 0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * ����ĳ��ĳ���µ�����
     * 
     * @param year ���
     * @param month �·�
     */
    public static int getDaySumOfMonth(int year, int month)
    {
        int days = 0;
        switch (month)
        {
            case 1:
                days = 31;
                break;
            case 2:
                if (DateUtil.isLeapYear(year))
                    days = 29;
                else
                    days = 28;
                break;
            case 3:
                days = 31;
                break;
            case 4:
                days = 30;
                break;
            case 5:
                days = 31;
                break;
            case 6:
                days = 30;
                break;
            case 7:
                days = 31;
                break;
            case 8:
                days = 31;
                break;
            case 9:
                days = 30;
                break;
            case 10:
                days = 31;
                break;
            case 11:
                days = 30;
                break;
            case 12:
                days = 31;
                break;
        }
        return days;
    }

    /**
     * ��ȡ���ĵĵ�ǰʱ��ĺ�����
     * 
     * @return long ��ǰʱ��ĺ�����
     */
    public static long currentSystemTimeMillis()
    {
        long currentSystemTime = getLongDateTime(System.currentTimeMillis() + 8 * 60 * 60 * 1000
                + adjustTime); // GMT+8:00
        return currentSystemTime;
    } // end currentTimeMillis()

    /**
     * ��ȡ���ĵĵ�ǰʱ��ĺ�����
     * 
     * @return long ��ǰʱ��ĺ�����
     */
    public static long getLocalCurrentTimeMillis()
    {
        return getLongDateTime(System.currentTimeMillis() + 8 * 60 * 60 * 1000); // GMT+8:00
    } // end currentTimeMillis()

    /**
     * �Ѻ�����ת�����������ں�ʱ�䡣
     * 
     * @param millis ����1970��1��1����ҹ0��ĺ�����
     * @return ���ظ�ʽΪ yyyymmddhhmmss ���ַ����ں�ʱ�䡣
     */
    public static long getLongDateTime(long millis)
    {
        if (millis <= 0)
            return -1;
        Calendar rightNow = getCalendar();
        rightNow.setTime(new java.util.Date(millis));
        int year = rightNow.get(Calendar.YEAR);
        int month = rightNow.get(Calendar.MONTH) + 1;
        int day = rightNow.get(Calendar.DAY_OF_MONTH);
        int hour = rightNow.get(Calendar.HOUR_OF_DAY);
        int minute = rightNow.get(Calendar.MINUTE);
        int second = rightNow.get(Calendar.SECOND);
        String strDateTime = year + (month < 10 ? "0" + month : month + "")
                + (day < 10 ? "0" + day : day + "") + (hour < 10 ? "0" + hour : hour + "")
                + (minute < 10 ? "0" + minute : minute + "")
                + (second < 10 ? "0" + second : second + "");
        return Long.parseLong(strDateTime);
    } // end getLongDateTime(long millis)

    /**
     * ���ر��µ�һ������ڡ�
     * 
     * @param strDate �ַ������ڣ���ʽ��yyyy-mm-dd��
     * @return ���ظ�ʽ��yyyy-mm-dd
     */
    public static String getFirstDateOfMonth(String strDate)
    {
        return strDate.substring(0, 8) + "01";
    } // end getFirstDateOfMonth(String strDate)

    /**
     * ���ر������һ������ڣ����ظ�ʽ��yyyy-mm-dd��
     * 
     * @param strDate �ַ������ڣ���ʽ��yyyy-mm-dd��
     * @return ���ظ�ʽ��yyyy-mm-dd
     */
    public static String getLastDateOfMonth(String strDate)
    {
        long millis = getMillis(strDate);
        int year = getYear(millis);
        int month = getMonth(millis);
        int day = getDaySumOfMonth(year, month);
        return strDate.substring(0, 8) + day;
    } // end getLastDateOfMonth(String strDate)

    /**
     * �������µ�һ������ڡ�
     * 
     * @param strDate �ַ������ڣ���ʽ��yyyy-mm-dd��
     * @return ���ظ�ʽ��yyyy-mm-dd
     */
    public static String getFirstDateOfLastMonth(String strDate)
    {
        long millis = getMillis(strDate);
        int year = getYear(millis);
        int month = getMonth(millis);
        int lastMonth = month - 1;
        if (lastMonth == 0)
        {
            lastMonth = 12;
            year = year - 1;
        }
        return String.valueOf(year) + "-"
                + (lastMonth > 9 ? String.valueOf(lastMonth) : "0" + lastMonth) + "-01";
    } // end getFirstDateOfLastMonth(strDate)

    /**
     * �����������һ������ڡ�
     * 
     * @param strDate �ַ������ڣ���ʽ��yyyy-mm-dd��
     * @return ���ظ�ʽ��yyyy-mm-dd
     */
    public static String getLastDateOfLastMonth(String strDate)
    {
        long millis = getMillis(strDate);
        int year = getYear(millis);
        int month = getMonth(millis);
        // ���������·�
        month = month - 1;
        if (month == 0)
        {
            month = 12;
            year = year - 1;
        }
        int day = getDaySumOfMonth(year, month);
        return "" + year + "-" + ((month > 9 ? String.valueOf(month) : "0" + month)) + "-"
                + String.valueOf(day);
    } // end getLastDateOfLastMonth(strDate)

    /**
     * ���һ���׼ʱ���һ�����׼ʱ���������� �õ�һ����ǰ������ʱ��
     * 
     * @param baseDate �ַ������͵Ļ�׼ʱ��
     * @param days ���׼ʱ������������/����
     * @param �����ַ������ڣ���ʽΪ��yyyy-mm-dd��
     */
    public static String getAdvancedDate(String baseDate, int days)
    {
        // ȡ������ĺ�����
        long realMillis = dateMillis(baseDate);
        realMillis += days * 86400000l;
        return getDate(getLongDateTime(realMillis));
    }

    /**
     * ��һ����ʽ�������ַ�ָ����������͡�
     * 
     * @param date String ��ʾ������ݵ��ַ�
     * @param format String �����ַ�ĸ�ʽ
     * @return Date ���ص������������
     */
    public static java.util.Date parseDate(String date, String format)
    {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        ParsePosition pos = new ParsePosition(0);
        java.util.Date d = formatter.parse(date, pos);
        return d;
    }

    /**
     * ������ת����ָ����ʽ���ַ�.
     * 
     * @param d Date Ҫ��ת�����������
     * @param format String ָ����ת����ʽ
     * @return String ��ʾת��֮��������ַ�
     */
    public static String format(Date d, String format)
    {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String s = formatter.format(d);
        return s;
    }

    /**
     * ������ת�����ַ��ʽ��Ĭ�ϸ�ʽΪ��yyyyMMddHHmmssS
     * 
     * @param d Date Ҫ��ת�����������
     * @return String ��ʾת��֮��������ַ�
     */
    public static String format(Date d)
    {
        return format(d, "yyyyMMddHHmmssS");
    }

    /**
     * �ֽ�ʱ���ַ�ת������
     * 
     * @param timestr String ����22-11:06:23 ��00:08��
     * @return long
     */
    public static long parseTimeToSecond(String timestr)
    {
        long time = 0L;
        try
        {
            if (timestr.indexOf(":") != -1)
            {
                String[] times = com.phevos.common.util.StringUtil.splitStr(timestr, ":");
                // �ֽ�����22-11:06:23�����
                if (times != null && times.length == 3)
                {
                    if (!(times[0].indexOf("-") == -1))
                    {
                        String[] days = times[0].split("-");
                        time = Long.parseLong(days[0]) * 24 * 3600 + Long.parseLong(days[1]) * 3600;
                    }
                    else
                    {
                        time = Long.parseLong(times[0]) * 3600;
                    }
                    time += Long.parseLong(times[1]) * 60 + Long.parseLong(times[2]);
                }
                else if (times.length == 2)
                { // �ֽ�����00:08�����
                    time = Long.parseLong(times[0]) * 60 + Long.parseLong(times[1]);
                }
            }
            else
            { // ����ֻ��������
                return Long.parseLong(timestr);
            }

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return Long.MIN_VALUE;
        }
        return time;
    }

    /**
     * ����ת����ʱ���ַ�
     * 
     * @param second long
     * @return String
     */
    public static String parseSecondToTime(long second)
    {
        String strReturn = "";
        long DAYSEC = 3600 * 24;
        long HOURSEC = 3600;
        long MINSEC = 60;
        int day = 0;
        int hour = 0;
        int min = 0;
        int sec = 0;
        // ����һ��
        // if(second > DAYSEC)
        {
            day = (int)(second / DAYSEC);

            long sectemp = second % DAYSEC;
            hour = (int)(sectemp / HOURSEC);

            sectemp = sectemp % HOURSEC;
            min = (int)(sectemp / MINSEC);

            sec = (int)(sectemp % MINSEC);
        }
        // �������
        if (day > 0)
        {
            strReturn += day + "-";
        }
        // ���Сʱ
        if (hour > 9)
        {
            strReturn += hour;
        }
        else
        {
            strReturn += "0" + hour;
        }
        // ��ӷ���
        if (min > 9)
        {
            strReturn += ":" + min;
        }
        else
        {
            strReturn += ":0" + min;
        }
        // �����
        if (sec > 9)
        {
            strReturn += ":" + sec;
        }
        else
        {
            strReturn += ":0" + sec;
        }
        return strReturn;
    }

    /** ************************** private methods *************************** */
    private static Calendar getCalendar()
    {
        return Calendar.getInstance();
    }

    /**
     * ���������ڣ�yyyymmddhhmmss��ת���ɺ�����
     */
    public static long getDateMillis(long millis)
    {
        return dateMillis(getDateTime(millis));
    } // end getDateMillis(long millis)

    /**
     * �Ѹ�ʽΪ yyyy/mm/dd �� yyyy-mm-dd ������ת���ɺ����� ���߰Ѹ�ʽΪ yyyy/mm/dd hh:mm:ss �� yyyy-mm-dd
     * hh:mm:ss ������ʱ��ת���ɺ�����
     * 
     * @param strDate ��ʾһ�������ַ���ʽΪ yyyy/mm/dd �� yyyy-mm-dd ��
     * @return ���ؾ���1970��1��1����ҹ0��ĺ�����
     */
    private static long dateMillis(String strDate)
    {
        if (strDate == null || (strDate != null && strDate.trim().equals("")))
            return -1;
        String sDate = strDate;
        String sTime = null;
        String sep = " ";
        StringTokenizer st = null;
        long millis = 0;
        if (strDate.indexOf(":") > 0)
        {
            st = new StringTokenizer(strDate, sep);
            sDate = st.nextToken();
            sTime = st.nextToken();
        }
        if (sTime != null)
        {
            st = new StringTokenizer(sTime, ":");
            int hour = Integer.parseInt(st.nextToken());
            int minute = Integer.parseInt(st.nextToken());
            int second = Integer.parseInt(st.nextToken());
            millis = hour * 60 * 60 * 1000 + minute * 60 * 1000 + second * 1000;
        }
        try
        {
            sDate = getFormatDate(sDate);
            sDate = sDate.toString().replace('/', seperator);
            if (sDate.equals("1970-01-01"))
                millis += 0;
            else

                // GMT+8:00
                millis = java.sql.Date.valueOf(sDate).getTime() + 28800000 + millis;
            return millis;
        }
        catch (Throwable e)
        {
        }
        return -1;
    }

    /**
     * add by wangcp ��Ӧbs�ĳ�ʼ����ѯ�������ʱ��ķ��� para dataFormat: yyyy-MM-dd HH:mm�� yyyy-MM-dd�ȸ�ʽ
     * ���ص�ʱϵͳʱ��
     */
    public static String getNowDate(String dataFormat)
    {
        java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(dataFormat);
        Date odate = new Date();
        Date sdate = new Date(odate.getTime());
        return dateFormat.format(sdate);
    }

    /**
     * ��ȡ������N��ǰ�ķ���
     * 
     * @param preDay
     * @param dataFormat
     * @return
     */
    public static String getPreDate(int preDay, String dataFormat)
    {
        java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(dataFormat);
        long currentTime = System.currentTimeMillis();
        long preTime = currentTime - 86400000;
        Date odate = new Date(preTime);
        return dateFormat.format(odate);
    }

    public static String getFirstDayOfMonth(String dataFormat)
    {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return format(cal.getTime(), dataFormat);
    }

    /**
     * This method generates a string representation of a date based on the System Property
     * 'dateFormat' in the format you specify on input
     * 
     * @param aDate A date to convert
     * @return a string representation of the date
     */
    public static String convertDateToString(Date aDate)
    {
        return getDateTime(getDatePattern(), aDate);
    }

    public static String getDatePattern()
    {
        Locale locale = LocaleContextHolder.getLocale();
        String defaultDatePattern;
        try
        {
            defaultDatePattern = ResourceBundle.getBundle(Constants.BUNDLE_KEY, locale).getString(
                    "date.format");
        }
        catch (MissingResourceException mse)
        {
            defaultDatePattern = "MM/dd/yyyy";
        }

        return defaultDatePattern;
    }

    public static String getDateTimePattern()
    {
        return DateUtil.getDatePattern() + " HH:mm:ss.S";
    }

    /**
     * This method generates a string representation of a date's date/time in the format you specify
     * on input
     * 
     * @param aMask the date pattern the string is in
     * @param aDate a date object
     * @return a formatted string representation of the date
     * @see java.text.SimpleDateFormat
     */
    public static String getDateTime(String aMask, Date aDate)
    {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate == null)
        {
            log.error("aDate is null!");
        }
        else
        {
            df = new SimpleDateFormat(aMask);
            returnValue = df.format(aDate);
        }

        return (returnValue);
    }

    public static Date convertStringToDate(String aMask, String strDate) throws ParseException
    {
        SimpleDateFormat df;
        Date date;
        df = new SimpleDateFormat(aMask);

        if (log.isDebugEnabled())
        {
            log.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");
        }

        try
        {
            date = df.parse(strDate);
        }
        catch (ParseException pe)
        {
            // log.error("ParseException: " + pe);
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());
        }

        return (date);
    }

    /**
     * This method converts a String to a date using the datePattern
     * 
     * @param strDate the date to convert (in format MM/dd/yyyy)
     * @return a date object
     * @throws ParseException when String doesn't match the expected format
     */
    public static Date convertStringToDate(String strDate)
      throws ParseException {
        Date aDate = null;

        try {
            if (log.isDebugEnabled()) {
                log.debug("converting date with pattern: " + getDatePattern());
            }

            aDate = convertStringToDate(getDatePattern(), strDate);
        } catch (ParseException pe) {
            log.error("Could not convert '" + strDate + "' to a date, throwing exception");
            pe.printStackTrace();
            throw new ParseException(pe.getMessage(),
                                     pe.getErrorOffset());
        }

        return aDate;
    }
}
